﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.Common;
using ZSZ.DTO;
using ZSZ.Iservice;
using ZSZ.Service.Entity;

namespace ZSZ.Service
{
    public class UserService : IUserService
    {
        public long AddNew(string phoneNum, string passWord)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<UserEntity> cs = new CommonService<UserEntity>(ct);
                bool exists = cs.GetAll().Any(u => u.PhoneNum == phoneNum);
                if (exists)
                {
                    throw new ArgumentException("这个手机号已经存在用户了");
                }
                UserEntity en = new UserEntity();
                en.PhoneNum = phoneNum;
                string yan = CommonHelper.GenerateCaptchaCode(5);
                en.PassWordYan = yan;
                en.PassWordHash = (passWord + yan).CalcMD5();
                en.LastLoginErrorTime = DateTime.Now;
                en.LoginErrorTocal = 0;
                ct.Users.Add(en);
                ct.SaveChanges();
                return en.Id;
            }
        }

        public bool CheckLogin(string phoneNum, string passWord)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<UserEntity> cs = new CommonService<UserEntity>(ct);
                var exists = cs.GetAll().SingleOrDefault(u => u.PhoneNum == phoneNum);
                if (exists == null)
                {
                    throw new ArgumentException("这个手机号没有用户");
                }
                string yan = exists.PassWordYan;
                return exists.PassWordHash == (passWord + yan).CalcMD5();
            }
        }
        private UserDTO ToDTO(UserEntity e)
        {
            UserDTO dto = new UserDTO();
            dto.CityId = e.CityId;
            dto.CreateTime = e.CreateDatatime;
            dto.Id = e.Id;
            dto.LastLoginErrorDateTime = e.LastLoginErrorTime;
            dto.LoginErrorTimes = (int)e.LoginErrorTocal;
            dto.PhoneNum = e.PhoneNum;
            return dto;
        }
        public UserDTO GetById(long id)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<UserEntity> cs = new CommonService<UserEntity>(ct);
                var en = cs.GetById(id);
                if (en == null)
                {
                    throw new ArgumentException("没有这个user");
                }
                return ToDTO(en);
            }
        }

        public UserDTO GetByPhoneNum(string phoneNum)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<UserEntity> cs = new CommonService<UserEntity>(ct);
                var en = cs.GetAll().SingleOrDefault(u => u.PhoneNum == phoneNum);
                if (en == null)
                {
                    throw new ArgumentException("没有这个user");
                }
                return ToDTO(en);
            }
        }

        public void SetUserCityId(long userId, long cityId)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<UserEntity> cs = new CommonService<UserEntity>(ct);
                var en = cs.GetById(userId);
                if (en == null)
                {
                    throw new ArgumentException("没有这个user");
                }
                en.CityId = cityId;
                ct.SaveChanges();
            }
        }

        public void UpdatePwd(long userId, string newPassword)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<UserEntity> cs = new CommonService<UserEntity>(ct);
                var en = cs.GetById(userId);
                if (en == null)
                {
                    throw new ArgumentException("没有这个user");
                }
                en.PassWordHash = (newPassword+en.PassWordYan).CalcMD5();
                ct.SaveChanges();
            }
        }

        public void AddError(long id)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<UserEntity> cs = new CommonService<UserEntity>(ct);
                var en = cs.GetById(id);
                if (en == null)
                {
                    throw new ArgumentException("没有这个user");
                }
                en.LoginErrorTocal += 1;
                en.LastLoginErrorTime = DateTime.Now;
                ct.SaveChanges();
            }
        }

        public void ResetError(long id)
        {
            using (ZSZDBcontext ct = new ZSZDBcontext())
            {
                CommonService<UserEntity> cs = new CommonService<UserEntity>(ct);
                var en = cs.GetById(id);
                if (en == null)
                {
                    throw new ArgumentException("没有这个user");
                }
                en.LoginErrorTocal = 0;
                en.LastLoginErrorTime = DateTime.Now;
                ct.SaveChanges();
            }
        }
    }
}
